package com.mall.b2c.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.domain.member.SiteNoticeTextDomain;
import com.mall.b2c.enums.SiteNoticeType;
import com.mall.b2c.service.SiteNoticeService;
import com.mall.b2c.service.SiteNoticeTextService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.b2c.util.ShopUtil;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.util.EnumUtils;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

/**
 * 站内通知
 * @author lzx
 *
 */
@Controller
@RequestMapping("/admin/b2c/siteNotice")
public class SiteNoticeController extends BaseController{
	
	@Resource private SiteNoticeService siteNoticeService;
	@Resource private SiteNoticeTextService siteNoticeTextService;
	
	@RequestMapping("/textlist")
	public String textlist(Model model) {
		model.addAttribute("siteNoticeTypeList", EnumUtils.enumProp2I18nList(SiteNoticeType.class));
		return "/admin/b2c/sitenotice/textlist";
	}
	
	@RequestMapping(value="/queryTextData")
	@ResponseBody
	public Pager queryTextData(Pager pager, HttpServletRequest request) {
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
		pager = siteNoticeTextService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	@Validations(
			requiredFields = { 
				@RequiredFieldValidator(fieldName = "ids", message = "信息不存在")
			}
	)
	@RequestMapping("/delText")
	public String deleteText(Long[] ids, HttpServletResponse response) {
		siteNoticeTextService.remove(ids);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String add(Model model){
		model.addAttribute("siteNoticeTypeList", EnumUtils.enumProp2I18nList(SiteNoticeType.class));
		return "/admin/b2c/sitenotice/input";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String add(Long[] memberIds, SiteNoticeTextDomain noticeText, HttpServletResponse response){
		siteNoticeService.save(noticeText, memberIds, ShopUtil.getUser().getUsername());
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "id", message = "消息不存在")
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String update(Long id, Model model){
		model.addAttribute("noticeText", siteNoticeTextService.find(id));
		model.addAttribute("memberList", siteNoticeService.findMemberList(id));
		
		model.addAttribute("siteNoticeTypeList", EnumUtils.enumProp2I18nList(SiteNoticeType.class));
		return "/admin/b2c/sitenotice/input";
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(SiteNoticeTextDomain noticeText, HttpServletResponse response){
		siteNoticeService.edit(noticeText, ShopUtil.getUser().getUsername());
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	@RequestMapping("/list")
	public String list(Long textId, Model model) {
		model.addAttribute("textId", textId);
		return "/admin/b2c/sitenotice/list";
	}
	
	@RequestMapping(value="/queryData")
	@ResponseBody
	public Pager queryData(Long textId, Pager pager, HttpServletRequest request) {
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.add(SearchFilter.eq("noticeText.id", textId));
		filters.add(SearchFilter.isNotNull("member"));
		pager = siteNoticeService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	@Validations(
			requiredFields = { 
				@RequiredFieldValidator(fieldName = "ids", message = "信息不存在")
			}
	)
	@RequestMapping("/delete")
	public String delete(Long[] ids, HttpServletResponse response) {
		siteNoticeService.remove(ids);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
	@RequestMapping(value="/correlate/{textId}", method=RequestMethod.GET)
	public String correlateMember(@PathVariable Long textId, Model model){
		model.addAttribute("textId", textId);
		return "/admin/b2c/sitenotice/correlate";
	}
	
	@Validations(
			requiredFields = { 
				@RequiredFieldValidator(fieldName = "textId", message = "消息不存在"),
				@RequiredFieldValidator(fieldName = "memberIds", message = "会员不存在")
			}
	)
	@RequestMapping(value="/correlate", method=RequestMethod.POST)
	public String correlateMember(Long textId, Long[] memberIds, HttpServletResponse response){
		siteNoticeService.correlate(textId, memberIds, ShopUtil.getUser().getUsername());
		return ResponseUtils.renderJSON(successMessage, response);
	}
}
